package net.zoneland.knowledge.controller;

import net.zoneland.knowledge.constant.DocumentVerificationConstants;
import net.zoneland.knowledge.controller.bean.TermInfo;
import net.zoneland.knowledge.model.ResponseResult;
import net.zoneland.knowledge.service.TermsInfoManagementService;
import net.zoneland.knowledge.utils.OplogUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Objects;

/**
 * 文稿校验-名词管理.
 */
@RestController
@RequestMapping("/documentVerification/termManagement")
public class DocumentTermManagementController extends AbstractBaseController {

    /**
     * 日志.
     */
    private static final Logger LOGGER = LoggerFactory.getLogger(DocumentTermManagementController.class);

    /**
     * 名词信息管理服务.
     */
    @Resource
    private TermsInfoManagementService termsInfoManagementService;

    /**
     * 删除名词（批量）.
     * @param request 请求.
     * @param termIds 要删除的名词id,多个以逗号分隔 aa,bb,cc...
     * @return 返回删除成功还是失败.
     */
    @PostMapping("/deleteTermsBatch")
    public ResponseResult deleteTermsBatch(final HttpServletRequest request, @RequestBody final String[] termIds) {
        Assert.isTrue(termIds != null && termIds.length > 0, "id不能为空");
        final ResponseResult res = new ResponseResult();
        try {
            getAndCheckCurrentUser(request);
            termsInfoManagementService.deleteByPrimaryKey(termIds);
        } catch (final Exception e) {
            res.setResult(ResponseResult.FAILED_RESULT);
            res.setMessage(DocumentVerificationConstants.DELETE_TERMS_ERROR + e.getMessage());
            LOGGER.error(DocumentVerificationConstants.DELETE_TERMS_ERROR, e);
        }
        return res;
    }

    /**
     * 查询名词.
     * @param request 请求.
     * @param termInfo 要查询的名词信息.
     * @return 返回查询到的名词列表.
     */
    @PostMapping("/queryTerms")
    public ResponseResult queryTerms(final HttpServletRequest request, @RequestBody final TermInfo termInfo) {
        final ResponseResult res = new ResponseResult();
        try {
            getAndCheckCurrentUser(request);
            res.setData(termsInfoManagementService.queryTermsLike(termInfo));
        } catch (final Exception e) {
            res.setResult(ResponseResult.FAILED_RESULT);
            res.setMessage(DocumentVerificationConstants.QUERY_TERMS_ERROR + e.getMessage());
            LOGGER.error(DocumentVerificationConstants.QUERY_TERMS_ERROR, e);
        }
        return res;
    }

    /**
     * 新增名词.
     * @param request 请求.
     * @param termInfo 要新增的名词对象.
     * @return 返回成功失败.
     */
    @PostMapping("/addTerm")
    public ResponseResult addTerm(final HttpServletRequest request, @RequestBody final TermInfo termInfo) {
        Assert.isTrue(Objects.nonNull(termInfo) && Objects.nonNull(termInfo.getTermName()) && Objects.nonNull(termInfo.getTermType()), "名词名称、名词类型都不能为空");
        final ResponseResult res = new ResponseResult();
        try {
            getAndCheckCurrentUser(request);
            termInfo.setId(OplogUtils.getUuid32());
            termsInfoManagementService.addTerm(termInfo);
        } catch (final Exception e) {
            res.setResult(ResponseResult.FAILED_RESULT);
            res.setMessage(DocumentVerificationConstants.ADD_TERM_ERROR + e.getMessage());
            LOGGER.error(DocumentVerificationConstants.ADD_TERM_ERROR, e);
        }
        return res;
    }

    /**
     * 修改名词信息.
     * @param request 请求.
     * @param termInfo 要更新的名词对象.
     * @return 返回成功失败.
     */
    @PostMapping("/updateTerm")
    public ResponseResult updateTerm(final HttpServletRequest request, @RequestBody final TermInfo termInfo) {
        Assert.isTrue(Objects.nonNull(termInfo) && StringUtils.isNotBlank(termInfo.getId()), " id不能为空");
        final ResponseResult res = new ResponseResult();
        try {
            getAndCheckCurrentUser(request);
            termsInfoManagementService.updateTermById(termInfo);
        } catch (final Exception e) {
            LOGGER.error(DocumentVerificationConstants.UPDATE_TERM_ERROR, e);
            res.setResult(ResponseResult.FAILED_RESULT);
            res.setMessage(DocumentVerificationConstants.UPDATE_TERM_ERROR + e.getMessage());
        }
        return res;
    }
}
